package com.example.calculation;


import com.example.entity.CalRecipe;
import com.example.entity.Chef;

/**
 * @author 胡帅博
 * @date 2020/9/28 22:50
 */
public class CacheKitchenGodCal{

    // 品质加成的所有可选值 -1代表不能做 0代表能做但是没加成 0.1代表加成是10%，依次类推
    private double[] qualityAdd = {-1, 0, 0.1, 0.3, 0.5,1.0};


    public CacheKitchenGodCal() {
        initDivisionResult();
    }

    private void initDivisionResult() {
        final int one = quality.length;
        final int two = quality[0].length;
        for (int i = 1; i < one; i++) {
            for (int i2 = 1; i2 < two; i2++) {
                quality[i][i2] = i / i2;
            }
        }
        for (int i = 1; i < one; i++) {
            quality[i][0] = -1;
        }
    }


    /**
     * 用于计算在没有厨具的情况下，一个菜可以达到的的品质
     * <p>
     * 虽然没有厨具，但需要考虑到，厨师可能会有一些 技法加成，本身不能做的菜谱，但如果这个菜得分很高，
     * 厨师带着加上100技法的厨具就可以做，则判定为品质加成为0，
     * 如果菜谱需要两项技法，而且厨师两项技法都不达标，则直接判定为不能做
     */

    public double QualityAddNoEquip(Chef chef, SkillEffect effect, CalRecipe recipe) {
        double ratio = 5, t;
        int faildCount = 0;
        double s;
        if (recipe.bake != 0) {
            s = (chef.bake + effect.bake) * (1 + effect.bakePercent);
            t = quality[(int) s][recipe.bake];
            ratio = t < ratio ? t : ratio;
            if (ratio < 1) {
                faildCount++;
                ratio = 1;
            }
        }
        if (recipe.boil != 0) {
            s = (chef.boil + effect.boil) * (1 + effect.boilPercent);
            t = quality[(int) s][recipe.boil];
            ratio = t < ratio ? t : ratio;
        }
        if (recipe.stirfry != 0) {
            s = (chef.stirfry + effect.stirfry) * (1 + effect.stirfryPercent);
            t = quality[(int) s][recipe.stirfry];
            ratio = t < ratio ? t : ratio;
            if (ratio < 1) {
                faildCount++;
                ratio = 1;
            }
        }
        if (recipe.knife != 0) {
            s = (chef.knife + effect.knife) * (1 + effect.knifePercent);
            t = quality[(int) s][recipe.knife];
            ratio = t < ratio ? t : ratio;
            if (ratio < 1) {
                faildCount++;
                ratio = 1;
            }
        }
        if (recipe.fry != 0) {
            s = (chef.fry + effect.fry) * (1 + effect.fryPercent);
            t = quality[(int) s][recipe.fry];
            ratio = t < ratio ? t : ratio;
            if (ratio < 1) {
                faildCount++;
                ratio = 1;
            }
        }
        if (recipe.steam != 0) {
            s = (chef.steam + effect.steam) * (1 + effect.steamPercent);
            t = quality[(int) s][recipe.steam];
            ratio = t < ratio ? t : ratio;
            if (ratio < 1) {
                faildCount++;
                ratio = 1;
            }
        }

        if (faildCount > 1) {
            return -1;
        }else{
            int c = (int) ratio;
            c = c < 0 ? 0 : c;
            return qualityAdd[c];
        }

    }


    //使用 quality保存除法结果，减少除法次数
    private int[][] quality = new int[2000][400];

    // 用于精确计算 厨师对某个菜谱的品质
    public double QualityAdd(Chef chef, SkillEffect effect, CalRecipe recipe) {
        double ratio = 4, t;
        int i = 0;
        double s;
        if (recipe.bake != 0) {
            s = (chef.bake + effect.bake) * (1 + effect.bakePercent);
            if (s < 0) {
                return -1;
            } else {
                t = quality[(int) s][recipe.bake];
            }
            ratio = t < ratio ? t : ratio;
        }
        if (recipe.boil != 0) {
            s = (chef.boil + effect.boil) * (1 + effect.boilPercent);
            if (s < 0) {
                return -1;
            } else {
                t = quality[(int) s][recipe.boil];
            }
            ratio = t < ratio ? t : ratio;
        }
        if (recipe.stirfry != 0) {
            s = (chef.stirfry + effect.stirfry) * (1 + effect.stirfryPercent);
            if (s < 0) {
                return -1;
            } else {
                t = quality[(int) s][recipe.stirfry];
            }
            ratio = t < ratio ? t : ratio;
        }
        if (recipe.knife != 0) {
            s = (chef.knife + effect.knife) * (1 + effect.knifePercent);
            if (s < 0) {
                return -1;
            } else {
                t = quality[(int) s][recipe.knife];
            }
            ratio = t < ratio ? t : ratio;
        }
        if (recipe.fry != 0) {
            s = (chef.fry + effect.fry) * (1 + effect.fryPercent);
            if (s < 0) {
                return -1;
            } else {
                t = quality[(int) s][recipe.fry];
            }
            ratio = t < ratio ? t : ratio;
        }
        if (recipe.steam != 0) {
            s = (chef.steam + effect.steam) * (1 + effect.steamPercent);
            if (s < 0) {
                return -1;
            } else {
                t = quality[(int) s][recipe.steam];
            }
            ratio = t < ratio ? t : ratio;
        }
        int c = (int) ratio;
        c = c < 0 ? 0 : c;
        return qualityAdd[c];
    }


    //技能加成(厨师技能，厨具)
    public double skillAdd(SkillEffect effect, CalRecipe recipe) {
        double add = 0;
        if (recipe.bake != 0) {
            add += (effect.usebake);
        }
        if (recipe.boil != 0) {
            add += (effect.useboil);
        }
        if (recipe.stirfry != 0) {
            add += (effect.usestirfry);
        }
        if (recipe.knife != 0) {
            add += (effect.useknife);
        }
        if (recipe.fry != 0) {
            add += (effect.usefry);
        }
        if (recipe.steam != 0) {
            add += (effect.usesteam);
        }
        int[] tags = recipe.tags;
        add += (effect.usecreation * tags[0]) + (effect.usemeat * tags[1]) + (effect.usevegetable * tags[2]) + (effect.usefish * tags[3]);
        //金币售价加成
        //add+=effect.goldgain;
        return add;
    }


}
